package com.songlanyun.modules.balacegiveaway.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.modules.pointgiveaway.entity.PointGiveAwayEntity;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;

import com.songlanyun.modules.balacegiveaway.dao.BalanceGiveAwayDao;
import com.songlanyun.modules.balacegiveaway.entity.BalanceGiveAwayEntity;
import com.songlanyun.modules.balacegiveaway.service.BalanceGiveAwayService;


@Service("balanceGiveAwayService")
public class BalanceGiveAwayServiceImpl extends ServiceImpl<BalanceGiveAwayDao, BalanceGiveAwayEntity> implements BalanceGiveAwayService {

    @Override
    public BalanceGiveAwayEntity create(BalanceGiveAwayEntity entity) {
        this.baseMapper.insert(entity);
        return entity;
    }

    @Override
    public BalanceGiveAwayEntity modify(BalanceGiveAwayEntity entity) {
        return null;
    }

    @Override
    public void remove(Long id) {

    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        LambdaQueryWrapper<BalanceGiveAwayEntity> wrapper = new LambdaQueryWrapper<>();
        String giveRealName = MapUtil.getStr(params, "give_real_name");
        String giveMobile = MapUtil.getStr(params, "give_mobile");

        String acceptRealName = MapUtil.getStr(params, "accept_real_name");
        String acceptMobile = MapUtil.getStr(params, "accept_mobile");

        Date startTime = MapUtil.getDate(params, "start_time");
        Date endTime = MapUtil.getDate(params, "end_time");

        wrapper.like(StringUtils.isNotBlank(giveRealName),BalanceGiveAwayEntity::getGiveRealName,giveRealName);
        wrapper.like(StringUtils.isNotBlank(giveMobile),BalanceGiveAwayEntity::getGiveMobile,giveMobile);

        wrapper.like(StringUtils.isNotBlank(acceptRealName),BalanceGiveAwayEntity::getAcceptRealName,acceptRealName);
        wrapper.like(StringUtils.isNotBlank(acceptMobile),BalanceGiveAwayEntity::getAcceptMobile,acceptMobile);

        wrapper.ge(ObjectUtil.isNotNull(startTime),BalanceGiveAwayEntity::getCreateTime,startTime);
        if(ObjectUtil.isNotNull(endTime)) {
            endTime = DateUtils.addDays(endTime,1);
            wrapper.le(ObjectUtil.isNotNull(endTime), BalanceGiveAwayEntity::getCreateTime, endTime);
        }

        wrapper.orderByDesc(BalanceGiveAwayEntity::getCreateTime);
        IPage<BalanceGiveAwayEntity> page = this.page(
                new Query<BalanceGiveAwayEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

}
